package com.nearby.demand.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nearby.demand.R;
import com.nearby.demand.adapter.SimpleFragmentPagerAdapter;
import com.nearby.demand.ui.UiMain2;
import com.nearby.demand.ui.UiWorldRelease;
import com.nearby.demand.utils.KLog;

import java.util.ArrayList;
import java.util.List;

//消息
public class FragmentWorld extends BaseFragment implements OnClickListener,
        OnItemClickListener {


    private Activity ctx;
    private View layout, layout_head;
    public RelativeLayout errorItem;
    public TextView errorText;
    private UiMain2 parentActivity;
    private List<PageFragment> fragments = new ArrayList<PageFragment>();
    private SimpleFragmentPagerAdapter pagerAdapter;
    private ViewPager viewPager;

    private TabLayout tabLayout;

    @Override
    protected void lazyLoad() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (layout == null) {
            ctx = this.getActivity();
            parentActivity = (UiMain2) getActivity();

            layout = ctx.getLayoutInflater().inflate(R.layout.fragment_world,
                    null);




            pagerAdapter = new SimpleFragmentPagerAdapter(getChildFragmentManager(), getActivity(), fragments);
            Bundle args1 = new Bundle();
            args1.putInt("ARG_PAGE", 1);
            Bundle args2 = new Bundle();
            args2.putInt("ARG_PAGE", 2);
            Bundle args3 = new Bundle();
            args3.putInt("ARG_PAGE", 3);
            fragments.add(PageFragment.newInstance(args1));
            fragments.add(PageFragment.newInstance(args2));
            fragments.add(PageFragment.newInstance(args3));
            viewPager = (ViewPager) layout.findViewById(R.id.viewpager);
            viewPager.setAdapter(pagerAdapter);
            viewPager.setOffscreenPageLimit(3);
            tabLayout = (TabLayout) layout.findViewById(R.id.sliding_tabs);
            ImageView iv = (ImageView) layout.findViewById(R.id.iv);
            iv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), UiWorldRelease.class));

                }
            });
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                tab.setCustomView(pagerAdapter.getTabView(i));
                tab.setTag(i);
            }
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    KLog.i("GGGGG", "onTabSelected=" + tab.getPosition());
                    fragments.get(tab.getPosition()).onClickRequest(true, pagerAdapter);
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    KLog.i("GGGGG", "onTabUnselected=" + tab.getPosition());

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {
                    KLog.i("GGGGG", "onTabReselected=" + tab.getPosition());
                    //                    fragments.get(tab.getPosition()).onClickRequest();
                }
            });
            setOnListener();
        } else {
            ViewGroup parent = (ViewGroup) layout.getParent();
            if (parent != null) {
                parent.removeView(layout);
            }
        }
        return layout;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    private void setOnListener() {

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

}
